package com.wcy.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.domain.SysUser;
import com.wcy.model.R;
import com.wcy.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.naming.Name;

/**
 * <p>Title:SysUserController.java</p >
 * <p>Description: </p >
 * <p>Copyright: 公共服务与应急管理战略业务本部 Copyright(c)2020</p >
 * <p>Date:2021/9/21 11:41</p >
 *
 * @author wcy (wangchaoyanga@mail.taiji.com.cn)
 * @version 1.0
 */
@Api(tags = "员工管理")
@RequestMapping("/users")
@RestController
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页大小"),
            @ApiImplicitParam(name = "mobile",value = "员工的手机号码"),
            @ApiImplicitParam(name = "fullname",value = "员工全名")
    })
    @PreAuthorize("hasAuthority('sys_user_query')")
    public R<Page<SysUser>> findByPage(@ApiIgnore Page<SysUser> page,String mobile,String fullname){
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<SysUser> userPage= sysUserService.findByPage(page,mobile,fullname);
        return R.ok(userPage);
    }

    @PostMapping
    @PreAuthorize("hasAuthority('sys_user_create')")
    public R add(@RequestBody SysUser sysUser){
        boolean save= sysUserService.addSysUser(sysUser);
        if(save){
            return R.ok();
        }
        return R.fail("新增失败");
    }

    @PatchMapping
    @PreAuthorize("hasAuthority('sys_user_update')")
    public R update(@RequestBody SysUser sysUser){
        boolean update=sysUserService.updateSysUser(sysUser);
        if(update){
            return R.ok();
        }
        return R.fail("修改失败");
    }

    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('sys_user_delete')")
    public R delete(@RequestBody Long[] userIds){
        boolean delete=sysUserService.deleteSysUser(userIds);
        if(delete){
            return R.ok();
        }
        return R.fail("删除失败");
    }
}
